package com.springdoor.goods.service.impl;

import java.util.List;
import java.util.Map;

import com.springdoor.goods.domain.SprdGoodsMain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springdoor.goods.mapper.SprdGoodsConfigMapper;
import com.springdoor.goods.domain.SprdGoodsConfig;
import com.springdoor.goods.service.ISprdGoodsConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品参数信息Service业务层处理
 * 
 * @author llshs
 * @date 2024-04-30
 */
@Service
public class SprdGoodsConfigServiceImpl implements ISprdGoodsConfigService 
{
    @Autowired
    private SprdGoodsConfigMapper goodsConfigMapper;

    /**
     * 查询商品参数信息
     * 
     * @param gConfigId 商品参数信息主键
     * @return 商品参数信息
     */
    @Override
    public SprdGoodsConfig selectSprdGoodsConfigByGConfigId(Long gConfigId)
    {
        return goodsConfigMapper.selectSprdGoodsConfigByGConfigId(gConfigId);
    }

    /**
     * 查询商品参数信息列表
     * 
     * @param sprdGoodsConfig 商品参数信息
     * @return 商品参数信息
     */
    @Override
    public List<SprdGoodsConfig> selectSprdGoodsConfigList(SprdGoodsConfig sprdGoodsConfig)
    {
        return goodsConfigMapper.selectSprdGoodsConfigList(sprdGoodsConfig);
    }

    /**
     * 新增商品参数信息
     * 
     * @param sprdGoodsMain 商品参数信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSprdGoodsConfig(SprdGoodsMain sprdGoodsMain)
    {
        List<SprdGoodsConfig> configList = sprdGoodsMain.getSprdGoodsConfigList();
        String gNumber = sprdGoodsMain.getgNumber();
        int i = goodsConfigMapper.deleteSprdGoodsConfigByGNumber(gNumber);
        if (configList != null && !configList.isEmpty()) {
            String username = sprdGoodsMain.getUpdateBy();
            configList.forEach(config -> {
                config.setgNumber(gNumber);
                if (StringUtils.isEmpty(config.getCreateBy())) {
                    config.setCreateBy(username);
                }
                config.setUpdateBy(username);
            });
            return goodsConfigMapper.batchSprdGoodsConfig(configList);
        }
        return i;
    }
}
